package com.atguigu.gmall.order.biz.service.impl;

import com.atguigu.gmall.common.constant.RedisConstant;
import com.atguigu.gmall.common.execption.GmallException;
import com.atguigu.gmall.common.result.ResultCodeEnum;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.common.util.DateUtil;
import com.atguigu.gmall.common.util.JsonUtils;
import com.atguigu.gmall.common.util.MD5;
import com.atguigu.gmall.model.activity.SeckillGoods;
import com.atguigu.gmall.model.mq.SeckillOrderMsg;
import com.atguigu.gmall.model.order.OrderDetail;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.order.biz.service.OrderSeckillService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Date;

@Service
public class OrderSeckillServiceImpl implements OrderSeckillService {



    @Autowired
    StringRedisTemplate redisTemplate;

    @Override
    public void prepareSeckillOrder(SeckillOrderMsg msg) {
        SeckillGoods goods = msg.getGoods();
        Long skuId = goods.getSkuId();

        //1、准备订单info
        OrderInfo orderInfo = new OrderInfo();

        //订单的金额是秒杀商品的金额
        orderInfo.setTotalAmount(goods.getCostPrice());
        orderInfo.setUserId(msg.getUserId());
        orderInfo.setOrderComment("秒杀订单");
        orderInfo.setOutTradeNo("ATGUIGU-"+System.currentTimeMillis()+"-"+msg.getUserId());
        orderInfo.setImgUrl(goods.getSkuDefaultImg());
        orderInfo.setOrderDetailList(Lists.newArrayList());


        //2、准备详情信息
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setSkuId(skuId);
        orderDetail.setUserId(msg.getUserId());
        orderDetail.setSkuName(goods.getSkuName());
        orderDetail.setImgUrl(goods.getSkuDefaultImg());
        orderDetail.setOrderPrice(goods.getCostPrice());
        orderDetail.setSkuNum(1);
        orderDetail.setHasStock("1");
        orderDetail.setCreateTime(new Date());


        //3、设置好关系
        orderInfo.setOrderDetailList(Arrays.asList(orderDetail));



        //4、缓存到 redis
        //seckill:code:b39583c345e890f00b2ed517b1149e2f
        String jsonStr = JsonUtils.parseObjectToJsonStr(orderInfo);

        //修改 秒杀码对应的秒杀单的数据
        redisTemplate.opsForValue().set(RedisConstant.SECKILL_CODE_LOCK+msg.getCode(),jsonStr);



    }

    @Override
    public OrderInfo getSeckillOrder(String code, Long skuId) {
        Long userId = AuthContextHolder.getUserIdBo().getUserId();

        String encrypt = MD5.encrypt(skuId + "" + userId + "" + DateUtil.formatDate(new Date()));
        if(!encrypt.equals(code)){
            //不对的
            throw new GmallException(ResultCodeEnum.PERMISSION);
        }

        String json = redisTemplate.opsForValue().get(RedisConstant.SECKILL_CODE_LOCK + code);
        if(StringUtils.isEmpty(json)){
            return null;
        }
        OrderInfo orderInfo = JsonUtils.parseJsonStrToObject(json, new TypeReference<OrderInfo>() {
        });
        return orderInfo;
    }
}
